/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: managerQuery.c
> Author: KAIJIE.JIANG
> Mail: 
> Created Time: 2024年07月18日 星期四 02时28分42秒
***************************************************/

#include "managerQuery.h"

//通过id查询会员信息
int queryIdToVipInfo(FILE *fp,VIP_M *head,int n){
	fp = fopen("./vipInfo.txt","r");
	if(fp == NULL){
		logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
				__FILE__,__FUNCTION__,__LINE__,strerror(errno));
		exit(-1);
	}
	head = createManagerHead();
	head = vipInfoReadFile(fp,head);

	fclose(fp);

	VIP_M *cur = NULL;
	cur = head->next;
	while(cur != NULL){
		if(cur->data.id == n){
			printf("id:%d\tname:%s\tidNumber:%s\ttel:%s\tbalance:%.2f\tstate:%d\n",\
					cur->data.id,cur->data.name,cur->data.idNumber,cur->data.tel,\
					cur->data.balance,cur->data.state);
			break;
		}
		cur = cur->next;
	}

	if(cur == NULL){
		printf("未找到该会员！\n");
		n = 1;
	}

	freeManagerLink(head);
	return n;
}

//通过姓名查询会员信息 返回值：id
int queryNameToVipInfo(FILE *fp,VIP_M *head){
	int n = 0;
	char str[20] = {0};

	fp = fopen("./vipInfo.txt","r");
	if(fp == NULL){
		logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
				__FILE__,__FUNCTION__,__LINE__,strerror(errno));
		exit(-1);
	}
	head = createManagerHead();
	head = vipInfoReadFile(fp,head);

	fclose(fp);

	VIP_M *cur = NULL;
	cur = head->next;
	printf("请输入需要查询的姓名\n");
	printf("姓名:");
	scanf("%s",str);

	while(cur != NULL){
		if(strcmp(cur->data.name,str) == 0){
			n = cur->data.id;
			printf("id:%d\tname:%s\tidNumber:%s\ttel:%s\tbalance:%.2f\tstate:%d\n",\
					cur->data.id,cur->data.name,cur->data.idNumber,cur->data.tel,\
					cur->data.balance,cur->data.state);
			break;
		}
		cur = cur->next;
	}

	if(cur == NULL){
		printf("未找到该会员！\n");
		n = 1;
	}
	freeManagerLink(head);
	return n;
}

//通过身份证号查询会员信息 返回值：id
int queryIdNumberToVipInfo(FILE *fp,VIP_M *head){
	int n = 0;
	char str[20] = {0};

	fp = fopen("./vipInfo.txt","r");
	if(fp == NULL){
		logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
				__FILE__,__FUNCTION__,__LINE__,strerror(errno));
		exit(-1);
	}
	head = createManagerHead();
	head = vipInfoReadFile(fp,head);

	fclose(fp);

	VIP_M *cur = NULL;
	cur = head->next;
	printf("请输入需要查询的身份证号\n");
	printf("身份证号:");
	scanf("%s",str);
	if(strlen(str) == 18){
		while(cur != NULL){
			if(strcmp(cur->data.idNumber,str) == 0){
				n = cur->data.id;
				printf("id:%d\tname:%s\tidNumber:%s\ttel:%s\tbalance:%.2f\tstate:%d\n",\
						cur->data.id,cur->data.name,cur->data.idNumber,cur->data.tel,\
						cur->data.balance,cur->data.state);
				break;
			}
			cur = cur->next;
		}

		if(cur == NULL){
			printf("未找到该会员！\n");
			n = 1;
		}
	}else{
		printf("身份证号输入错误！\n");
	}
	freeManagerLink(head);
	return n;
}

//通过联系电话查询会员信息 返回值：id
int queryTelToVipInfo(FILE *fp,VIP_M *head){
	int n = 0;
	char str[12] = {0};

	fp = fopen("./vipInfo.txt","r");
	if(fp == NULL){
		logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
				__FILE__,__FUNCTION__,__LINE__,strerror(errno));
		exit(-1);
	}
	head = createManagerHead();
	head = vipInfoReadFile(fp,head);

	fclose(fp);

	VIP_M *cur = NULL;
	cur = head->next;
	printf("请输入需要查询的联系电话\n");
	printf("联系电话:");
	scanf("%s",str);
	if(strlen(str) == 11){
		while(cur != NULL){
			if(strcmp(cur->data.tel,str) == 0){
				n = cur->data.id;
				printf("id:%d\tname:%s\tidNumber:%s\ttel:%s\tbalance:%.2f\tstate:%d\n",\
						cur->data.id,cur->data.name,cur->data.idNumber,cur->data.tel,\
						cur->data.balance,cur->data.state);
				break;
			}
			cur = cur->next;
		}

		if(cur == NULL){
			printf("未找到该会员！\n");
			n = 1;
		}
	}else{
		printf("联系电话输入错误！\n");
	}
	freeManagerLink(head);
	return n;
}

